#define _CRT_SECURE_NO_WARNINGS 1
#include "Stack.h"
void Stack::StackInit()
{
	_arry = (DataType*)malloc(sizeof(DataType) * 2);
	if (_arry == nullptr)
	{
		perror("malloc");
		exit(-1);
	}
	_size = 0;
	_capacity = 2;
}
void Stack::Checkcapacity()
{
	if (_capacity == _size)
	{
		DataType newcapacity = _capacity * 2;
		DataType* tmp = (DataType*)realloc(_arry, sizeof(DataType) * newcapacity);
		if (tmp == nullptr)
		{
			perror("realoc");
			exit(-1);
		}
		_arry = tmp;
		_capacity = newcapacity;
	}
}
void Stack::StackPush(DataType x)
{
	//检查容量
	Checkcapacity();
	_arry[_size] = x;
	_size++;
}

void Stack::StackPop()
{
	if (!Empty())
	{
		return;
	}
	_size--;
}
int Stack::StackSize()
{
	return _size;
}
DataType Stack::Top()
{
	return _arry[_size-1];
}
bool Stack::Empty()
{
	return _size;
}
void Stack::StackPrint()
{
	for (int i = 0; i < _size; i++)
	{
		cout << _arry[i] << ' ';
	}
	cout << endl; 
}
void Stack::StackDestory()
{
	if (_arry)
	{
		free(_arry);
		_arry = nullptr;
	}
	_size = 0;
	_capacity = 0;

}